unit rate;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, ZAbstractRODataset, ZAbstractDataset,
  ZDataset;

type
  TFRate = class(TForm)
    DataSource1: TDataSource;
    ZQuery1: TZQuery;
    DBGrid1: TDBGrid;
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FRate: TFRate;

implementation

uses
  data;

{$R *.dfm}

procedure TFRate.FormActivate(Sender: TObject);
begin
  with ZQuery1 do begin
    SQL.Clear;
    SQL.Add('set @a:=0');
    ExecSQL;
  end;
  with ZQuery1 do begin
    sql.Clear;
    SQL.Add('select *, @a:=@a+1 from users, stats where (users.status = stats.s_key) and (users.group = "user") order by point desc');
    Open;
  end;
  DBGrid1.Columns[0].FieldName := '@a:=@a+1';
  DBGrid1.Columns[1].FieldName := 'family';
  DBGrid1.Columns[2].FieldName := 'name';
  DBGrid1.Columns[3].FieldName := 'dept';
  DBGrid1.Columns[4].FieldName := 's_name';
  DBGrid1.Columns[5].FieldName := 'level';
  DBGrid1.Columns[6].FieldName := 'point';
end;

procedure TFRate.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ZQuery1.Close;
  Action := caFree;
end;

procedure TFRate.DBGrid1TitleClick(Column: TColumn);
begin
  if Column.Index <> 0 then begin
    ZQuery1.SortedFields := Column.FieldName;
    if (Column.Index = 5) or (Column.Index = 6) then ZQuery1.SortType := stDescending
    else ZQuery1.SortType := stAscending;
  end
  else begin
    ZQuery1.SortedFields := 'point';
    ZQuery1.SortType := stDescending;
  end;
end;

procedure TFRate.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  If DataSource1.DataSet.FieldValues['@a:=@a+1'] = 1 then DBGrid1.Canvas.Font.Color := clRed;
  If DataSource1.DataSet.FieldValues['@a:=@a+1'] = 2 then DBGrid1.Canvas.Font.Color := clGreen;
  If DataSource1.DataSet.FieldValues['@a:=@a+1'] = 3 then DBGrid1.Canvas.Font.Color := clBlue;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

end.
